$(function () {
    console.log('yoo.js');
    // t('{"account":{"id":"6130037","type":50},"pageContext":"","countReg":true}');
    // console.log( chrome.devtools.network);


    // $('video')[0].play();
    // console.log($('video').attr('src'));


    listenToMsg("whatTodo", ({msg: o, replyFunc: e}) => {
        console.log(o.todo);
        switch (o.todo) {
            case 'downUrl':
                $('.txp_btn_play').trigger('click')

                setTimeout(function () {
                    $('.txp_btn_play').trigger('click')
                    getDownUrlCnt = 0;
                    sd(o.id);
                }, 1000)

                // getTabId(e => {
                //     console.log(e)
                //     sendMsgToExt("whatTodo", {todo: 'gogogo'}, e).then(()=>{
                //         console.log('send over')
                //     })
                // });
                break;
            case 'list':
                l();
                break;
            case 'play':
                console.log($('.txp_btn_play'));
                $('.txp_btn_play').trigger('click')
                break;
            case 'go':
                window.location.href = o.url;
                break;
            case 'rerush':
                window.location.reload(true);
                break;
        }

        // console.log(o);
        // console.log(e);
    })



    var
        userIndex = 0,
        postData = '',
        dUrlIndex = 0,
        getDownUrlCnt = 0,

        //获取没有下载链接的视频条目

        //更新下载链接
        sd = (id) => {
            // console.log($('video').attr('src'));

            getDownUrlCnt +=1;
            let downUrl = $('video').attr('src');

            if(downUrl == undefined || downUrl == ''){

                if(getDownUrlCnt <=5){
                    $('.txp_btn_play').trigger('click')
                    setTimeout(function () {
                        $('.txp_btn_play').trigger('click')
                        sd(id);
                    },1000)
                }else{
                    getTabId(e => {
                        sendMsgToExt("whatTodo", {todo: 'noDownUrl'}, e).then(()=>{
                            console.log('send over')
                        })
                    });
                }


            }else{
                getTabId(e => {
                    sendMsgToExt("whatTodo", {todo: 'downUrl', id :id, url : downUrl}, e).then(()=>{
                        console.log('send over')
                    })
                });
            }
        },
        //获取可采集的用户id列表
        u = (fn) => {
            $.ajax({
                type: "POST",
                url: 'http://d.yoo.com/User',
                // xhrFields: {
                //     withCredentials: true // 这里设置了withCredentials
                // },
                // header : header,
                data: {tp:'tv'},
                dataType: 'JSON',
                success: function (data) {
                    // console.log(data.data.pageContext);
                    console.log('uid', data);
                    fn(data);
                }
            });
        },
        //保存信息
        s = (data, fn) =>{
            if(data['errCode'] != 0){
                return;
            }
            data = data['collections'];
            data = JSON.stringify(data);
            // console.log(data);

            $.ajax({
                type: "POST",
                url: 'http://d.yoo.com/Save',
                // xhrFields: {
                //     withCredentials: true // 这里设置了withCredentials
                // },
                // header : header,
                // dataType:'json',
                data: {collections :data},
                success: function (data) {
                    // console.log(data.data.pageContext);
                    console.log(data);
                    fn();
                }

            });
        },
        //获取用户id列表后读取该用户的作品列表
        l = () => {
            u(function (uid) {
                postData = '{"account":{"id":"' + uid[userIndex]['uid'] + '","type":50},"pageContext":"","countReg":true}';

                upUDate(uid);

                // console.log(postData);return;
                ll(uid);

            });


        },
        upUDate = (uid) => {
            $.ajax({
                type: "POST",
                url: 'http://d.yoo.com/User/upUDate',
                // xhrFields: {
                //     withCredentials: true // 这里设置了withCredentials
                // },
                // header : header,
                // dataType:'json',
                data: {id :uid[userIndex]['id']},
                success: function (data) {
                    // console.log(data.data.pageContext);
                }

            });
        },
        //获取用户列表信息
        ll = (uid) =>{
            const url = 'https://hgaccess.video.qq.com/huoguo/user_work_list?vappid=49109510&vsecret=c1202d7f3ba41f86cdd2d3d1082605b4ed764c21e29520f3&callback=func&raw=1';

            $.ajax({
                type: "POST",
                url: url,
                // xhrFields: {
                //     withCredentials: true // 这里设置了withCredentials
                // },
                // header : header,
                data: postData,
                success: function (data) {
                    // console.log(data.data.pageContext);
                    postData = '{"account":{"id":"' + uid[userIndex]['uid'] + '","type":50},"pageContext":"","countReg":true}';
                    console.log(data.data.pageContext);
                    // return;
                    var reg = new RegExp('"' , "g");
                    postData = postData.replace('Context":""', 'Context":"' + data.data.pageContext.replace(reg, '\\"') + '"');
                    // console.log(postData);return;
                    s(data.data, function () {
                        console.log(data);
                        //没有下一页，
                        if(data.data.hasNextPage == false){
                            userIndex += 1;
                            upUDate(uid);
                            if(uid.length <= userIndex){
                                console.error('uid is null');
                                return;
                            }
                            postData = '{"account":{"id":"' + uid[userIndex]['uid'] + '","type":50},"pageContext":"","countReg":true}';
                        }
                        ll(uid);
                    });
                }

            });
        };



    // t();


});



